 %{
#define YYSTYPE int
#include <stdio.h>
//#include <iostream.h>
//#include <vector>
#include <string.h>

   //   using namespace std;

   char* current[100];
   int pos;
   int nState = 0;
   char* currentAutomata;
   char* variables;
   int nVariables = 0;
   
%}

%token T_OPEN_PAR, T_CLOSE_PAR, T_PONTVIRG, T_DOISPONTOS,T_DEVC
%token T_OPEN_CHAVE, T_CLOSE_CHAVE, T_IGUAL, T_ASPAS, T_ASPA
%token T_PERCENT, T_PERCENT_RESERVED, T_PERCENT_MASK, T_S, T_R
%token T_DOT, T_MASKREP
%token T_DC_SCML_PORT, T_DC_TLM_PORT, T_DC_CLK_PORT, T_DC_FORMAT, T_DC_SERVICE
%token T_MAIOR, T_MENOR, T_VIRG, T_READ, T_WRITE, T_DC_REG, T_DC_BUFFER, T_DC_DRV_MAP, T_DC_OS
%token T_DC_OS_MAP, T_OPEN, T_INIT, T_RELEASE, T_CLOSE, T_IOCTL, T_SEEK, T_DEV_CTOR
%token T_SET_ADDRESS, T_BINDTO, T_SET_ACTION, T_PRE, T_POS, T_WAIT_STATE, T_INTERRUPT, T_POLLING
%token T_OPER, T_IDENT, T_LETTER, T_DIGIT, T_INT, T_PLUS, T_UCLINUX
   /*%union {
     int       t_int;  /* For the lexical analyser. NUMBER tokens 
     char      ident;  /* For the lexical analyser. IDENT tokens 
   }*/
/*%type <t_int>   T_INT
%type <ident> T_IDENT
%type <t_int> address_def*/

/*%start nt_devc_spec*/

%%

input : T_DEVC T_OPEN_PAR T_IDENT T_CLOSE_PAR T_OPEN_CHAVE devc_obj_def T_CLOSE_CHAVE;

devc_obj_def : port_definition T_PONTVIRG devc_obj_def
| format_definition T_PONTVIRG devc_obj_def
| serv_definition T_PONTVIRG devc_obj_def
| sto_definition T_PONTVIRG devc_obj_def
| varmap_definition devc_obj_def
| os_definiton T_PONTVIRG devc_obj_def
| osmap_definition devc_obj_def
| ctor_definition;

port_definition : T_DC_SCML_PORT T_IDENT {printf("-TESTE %s|%d-\n", current[pos-1-0],pos);pos=0;}
| T_DC_TLM_PORT T_IDENT
| T_DC_CLK_PORT T_IDENT;

format_definition : T_DC_FORMAT T_IDENT T_IGUAL fmt_decl;

fmt_decl : params_fmt
| ret_fmt;

params_fmt : T_ASPAS param_fmt T_ASPAS;

param_fmt : T_PERCENT T_IDENT T_DOISPONTOS T_INT T_DOISPONTOS T_S param_fmt
| T_PERCENT T_IDENT T_DOISPONTOS T_INT param_fmt
| T_PERCENT_RESERVED T_DOISPONTOS T_INT param_fmt
| reg_fmt param_fmt
| T_PERCENT T_IDENT T_DOISPONTOS T_INT T_DOISPONTOS T_S
| T_PERCENT T_IDENT T_DOISPONTOS T_INT
| T_PERCENT_RESERVED T_DOISPONTOS T_INT
| reg_fmt;

ret_fmt : T_ASPAS T_PERCENT T_DOISPONTOS T_R T_ASPAS
| T_ASPAS T_PERCENT T_DOISPONTOS T_R T_DOISPONTOS T_S T_ASPAS;

reg_fmt : T_PERCENT T_IDENT T_DOISPONTOS T_INT T_PERCENT_MASK T_DOISPONTOS T_ASPA bits T_ASPA;

bits : T_MASKREP bits
| T_DOT bits
| T_MASKREP
| T_DOT;

serv_definition : T_DC_SERVICE T_MENOR T_IDENT T_MAIOR T_IDENT 
{
  currentAutomata = current[pos-1-0];

  pos=0;
}
| T_DC_SERVICE T_MENOR T_IDENT T_VIRG rw T_MAIOR T_IDENT;

sto_definition : reg_decl
| buffer_decl;

reg_decl : T_DC_REG T_MENOR T_IDENT T_MAIOR T_IDENT
| T_DC_REG T_MENOR T_IDENT T_VIRG rw T_MAIOR T_IDENT;

rw : T_READ
| T_WRITE;

buffer_decl : T_DC_BUFFER T_IDENT T_DOISPONTOS T_INT T_DOISPONTOS T_INT;

varmap_definition : T_DC_DRV_MAP T_IDENT T_OPEN_CHAVE varmap_decl T_CLOSE_CHAVE;

varmap_decl : varmap T_PONTVIRG
| varmap T_PONTVIRG varmap_decl;

varmap : T_IDENT T_IGUAL semivar;

semivar : T_IDENT
| T_IDENT T_DOT T_IDENT
| T_IDENT T_PLUS semivar
| T_IDENT T_DOT T_IDENT T_PLUS semivar;

os_definiton : T_DC_OS T_MENOR os_type T_MAIOR T_IDENT;

os_type : T_UCLINUX;

osmap_definition : T_DC_OS_MAP T_IDENT T_OPEN_CHAVE osimap_decl T_CLOSE_CHAVE;

osimap_decl : T_IDENT T_DOT posix_intf T_IGUAL T_IDENT T_PONTVIRG osimap_decl
| T_IDENT T_DOT posix_intf T_IGUAL T_IDENT T_PONTVIRG;

posix_intf : T_OPEN
| T_INIT
| T_WRITE
| T_READ
| T_RELEASE
| T_CLOSE
| T_IOCTL
| T_SEEK;

ctor_definition : T_DEV_CTOR T_OPEN_PAR T_IDENT T_CLOSE_PAR T_OPEN_CHAVE method_definition T_CLOSE_CHAVE;

method_definition : address_def T_PONTVIRG method_definition
| action_def method_definition
| bind_def T_PONTVIRG method_definition
| address_def T_PONTVIRG
| action_def
| bind_def T_PONTVIRG;

address_def : T_IDENT T_DOT T_SET_ADDRESS T_OPEN_PAR T_INT T_CLOSE_PAR {printf("End: %s\n",current[pos-1-0]);pos=0};

bind_def : T_IDENT T_DOT T_BINDTO T_OPEN_PAR T_IDENT T_CLOSE_PAR
| T_IDENT T_DOT T_BINDTO T_OPEN_PAR T_IDENT T_DOT T_IDENT T_CLOSE_PAR
|T_IDENT T_DOT T_BINDTO T_OPEN_PAR T_IDENT T_DOT T_IDENT T_DOT T_IDENT T_CLOSE_PAR;

action_def : T_IDENT T_DOT T_SET_ACTION T_OPEN_PAR attrib T_CLOSE_PAR T_OPEN_CHAVE expr T_CLOSE_CHAVE
| T_IDENT T_DOT T_SET_ACTION T_OPEN_PAR T_CLOSE_PAR T_OPEN_CHAVE expr T_CLOSE_CHAVE;

attrib : T_PRE
| T_POS;

expr : l_expr
| assign T_PONTVIRG
| sinc_decl T_PONTVIRG
| l_expr expr
| assign T_PONTVIRG expr
| sinc_decl T_PONTVIRG expr;

l_expr : cond T_OPEN_CHAVE expr T_CLOSE_CHAVE;

/*l_expr : T_IDENT T_OPER T_INT T_PONTVIRG
| T_IDENT T_DOT T_IDENT T_OPER T_INT T_PONTVIRG
| T_IDENT T_OPER T_INT T_OPEN_CHAVE expr T_CLOSE_CHAVE
| T_IDENT T_DOT T_IDENT T_OPER T_INT T_OPEN_CHAVE expr T_CLOSE_CHAVE;*/

assign : T_IDENT T_IGUAL T_INT {printf("Atribuicao: %s em %s\n", current[pos-1-0], current[pos-1-1]); pos=0;}
| T_IDENT T_DOT T_IDENT T_IGUAL T_INT {printf("Atribuicao: %s em %s\n", current[pos-1-0], current[pos-1-1]);pos=0;}
| T_IDENT T_DOT T_IDENT T_DOT T_IDENT T_IGUAL T_INT {printf("Atribuicao: %s em %s\n", current[pos-1-0], current[pos-1-1]);pos=0;};

sinc_decl : wait_decl
| intr_decl
| poll_decl;

wait_decl : T_WAIT_STATE T_OPEN_PAR cond T_CLOSE_PAR;

intr_decl : T_INTERRUPT T_OPEN_PAR cond T_CLOSE_PAR;

poll_decl : T_POLLING T_OPEN_PAR cond T_CLOSE_PAR;

cond : T_IDENT T_OPER T_INT {printf("Comparacao: se %s %s %s\n", current[pos-1-2],current[pos-1-1],current[pos-1-0]); pos=0;}
| T_IDENT T_DOT T_IDENT T_OPER T_INT {printf("Comparacao: se %s %s %s\n", current[pos-1-2],current[pos-1-1],current[pos-1-0]); pos=0;}
| T_IDENT T_MAIOR T_INT {printf("Comparacao: se %s > %s\n", current[pos-1-1],current[pos-1-0]); pos=0;}
| T_IDENT T_DOT T_IDENT T_MAIOR T_INT {printf("Comparacao: se %s > %s\n", current[pos-1-1],current[pos-1-0]); pos=0;}
| T_IDENT T_MENOR T_INT {printf("Comparacao: se %s < %s\n", current[pos-1-1],current[pos-1-0]); pos=0;}
| T_IDENT T_DOT T_IDENT T_MENOR T_INT {printf("Comparacao: se %s < %s\n", current[pos-1-1],current[pos-1-0]); pos=0;}
| /*empty*/;

/*cnoum : article noum {printf("cnoum2 %s %s",$1,$2);} | noum {$$ = "cnoum1";};*/

%%
void addParam(char* param){
  //  printf("Pos: %d, PAram: %s.\n",pos,param);
  current[pos] = param;
  //printf("((%s))",current[pos]);
  pos++;
}

